package seggio;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import static seggio.Seggio.id;

/**
 *
 * @author ale
 */
public class DOMxml {

    private int numCabine;
    private static String regione;
    private String ip_seggio;
    private int porta_seggio;
    private String ip_interno;
    private int porta_interno;
    //----
    
    private Node nNode;
    private NodeList nList;
    private String path;
    Document doc;
    
    public DOMxml(String path, String nodo) {
        this.path = path;
        try {

            File fXmlFile = new File(path);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            nList = doc.getElementsByTagName(nodo); ///es: "seggio" "cabina"

        } catch (ParserConfigurationException | SAXException | IOException | DOMException e) {
        }
    }

    public int numeroCabineDelSeggio(){
       for (int temp = 0; temp < nList.getLength(); temp++) {

            nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                numCabine = Integer.parseInt(eElement.getElementsByTagName("numeroCabine").item(0).getTextContent());
            }
        }
        return numCabine;
    }
    
    public int getIdSeggio() {

        for (int temp = 0; temp < nList.getLength(); temp++) {

            nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                id = Integer.parseInt(eElement.getElementsByTagName("id").item(0).getTextContent());
            }
        }
        return id;
    }

    public String getRegione() {
        for (int temp = 0; temp < nList.getLength(); temp++) {

            nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                regione = eElement.getElementsByTagName("regione").item(0).getTextContent();
            }
        }
        return regione;
    }

    public String getIPSeggio() {
        for (int temp = 0; temp < nList.getLength(); temp++) {

            nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                ip_seggio = eElement.getElementsByTagName("ip_seggio").item(0).getTextContent();
            }
        }
        return ip_seggio;

    }

    public int getPortaSeggio() {

        for (int temp = 0; temp < nList.getLength(); temp++) {

            nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                porta_seggio = Integer.parseInt(eElement.getElementsByTagName("porta_seggio").item(0).getTextContent());
            }
        }
        return porta_seggio;
    }

    public String getIPInterno() {

        for (int temp = 0; temp < nList.getLength(); temp++) {

            nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                ip_interno = eElement.getElementsByTagName("ip_interno").item(0).getTextContent();
            }
        }
        return ip_interno;
    }

    public int getPortaInterno() {
        for (int temp = 0; temp < nList.getLength(); temp++) {

            nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                porta_interno = Integer.parseInt(eElement.getElementsByTagName("porta_interno").item(0).getTextContent());
            }
        }
        return porta_interno;
    }
    
    public ArrayList<String> getIPCabina() {
        
        ArrayList ip_cabina = new ArrayList<>();
        
        for (int temp = 0; temp < nList.getLength(); temp++) {

            nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                String ipCab = eElement.getElementsByTagName("ip_cabina").item(0).getTextContent();
                ip_cabina.add(ipCab);
                
            }
        }
        return ip_cabina;
    }

    // riscrivo il contenuto nel file
    public void scriviNelXML() throws TransformerConfigurationException, TransformerException {

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(path));
        transformer.transform(source, result);

    }
}
